﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.JScript.Compiler;
using Microsoft.JScript.Compiler.ParseTree;

namespace Yuyi.PluginForJS.Addin
{
    public class FunctionList
    {
        private string _code;
        public FunctionList(string code)
        {
            this._code = code;
        }
        public IList<AbstractNode> LoadFunctionList()
        {
            using (StringReader reader = new StringReader(_code))
            {
                char[] buffer = new char[_code.Length];
                reader.Read(buffer, 0, buffer.Length);
                if (buffer != null && buffer.Length > 0)
                {
                    IList<AbstractNode> nodes = new List<AbstractNode>();
                    Parser parser = new Parser(buffer, true);
                    List<Comment> comments = new List<Comment>();
                    BindingInfo bindingInfo = new BindingInfo();
                    DList<Statement, BlockStatement> sourceElements = parser.ParseProgram(ref comments, ref bindingInfo);
                    nodes = Helper.CreateNodes(sourceElements);
                    return nodes;
                }
                return null;
            }
        }
    }
}
